/**
 * Created by zhoulin on 2019/4/3.
 */

var app = new Vue({
    el: '#app',
    data: {
        permissions:['brole:copy','brole:update'],
    },
    //进入页面初始化方法
    created: function () {
        var that = this;
        var token = sessionStorageUtils.getJson(storageKey.sessionStorageKey.token);
        if(empty(token)){
            layer.msg("<span style='color:red'>登录超时！请重新登录！</span>", {time: 1500, icon:5});
            setTimeout(function () {
                window.location.href = _LOGIN_PAGE;
            },1200)

            return ;
        }

        if(!empty(aGET.id)){
            that.getInfoById(aGET.id);
        }

    },
    methods: {
        //权限控制
        permission: function (index) {
            return (_PERMISSIONS.indexOf(this.permissions[index]) != -1);
        },
        getInfoById:function (id) {
            var that = this;

            Get(that,_HOST+_API.admin_sysRole_getInfoById+id)
                .then(function (response) {
                    // 响应成功回调
                    console.log(response)
                    if(response){
                        if(response.success){
                            var list = response.data
                            for (var item in list) {
                                if (item == 'status' ) {
                                    $("#Form select[name='" + item + "']").val(list[item]);
                                    continue;
                                }
                                $("#Form input[name='" + item + "']").val(list[item]);
                            }

                            $("#Form").data('bootstrapValidator').destroy();
                            $('#Form').data('bootstrapValidator', null);
                            Validator();
                        }else {
                            layer.msg(response.msg, {time: 1500, icon:5});
                        }
                    }
                })
        },
        //复制新增
        copy:function () {
            var that = this;
            var param = formToJson($("#Form").serialize());
            //console.log(param)
            Post(that,_HOST+_API.admin_sysBusinessRole_copy,param,true,that.permissions[0])
                .then(function (response) {
                    // 响应成功回调
                    console.log(response)
                    if(response){
                        if(response.success){
                            layer.msg(response.msg, {time: 1500, icon:1});
                            setTimeout(function () {
                                window.parent.location.reload(); //刷新父页面
                                layer.closeAll();
                            }, 1500)
                        }else {
                            layer.msg(response.msg, {time: 1500, icon:5});
                        }
                    }
                    $("#Form").data('bootstrapValidator').destroy();
                    $('#Form').data('bootstrapValidator', null);
                    Validator();
                })
        },
        update:function () {
            var that = this;
            var param = formToJson($("#Form").serialize());
            //console.log(param)
            Put(that,_HOST+_API.admin_sysBusinessRole_update,param,true,that.permissions[1])
                .then(function (response) {
                    // 响应成功回调
                    console.log(response)
                    if(response){
                        if(response.success){
                            layer.msg(response.msg, {time: 1500, icon:1});
                            setTimeout(function () {
                                window.parent.location.reload(); //刷新父页面
                                layer.closeAll();
                            }, 1500)
                        }else {
                            layer.msg(response.msg, {time: 1500, icon:5});
                        }
                    }
                    $("#Form").data('bootstrapValidator').destroy();
                    $('#Form').data('bootstrapValidator', null);
                    Validator();
                })
        }
    },
})

$('#Form').data('bootstrapValidator', null);
Validator();
function Validator() {
    $('#Form').bootstrapValidator({
        fields: {
            name: {
                validators: {
                    notEmpty: {
                        message: '角色名不能为空'
                    },
                    stringLength: {
                        max: 30,
                        message: '角色名长度最多30个字符'
                    },
                }
            },
            sort: {
                validators: {
                    notEmpty: {
                        message: '排序不能为空'
                    }
                }
            },
            description: {
                validators: {
                    notEmpty:{
                        message:'描述不能为空'
                    },
                    stringLength: {
                        max: 60,
                        message: '描述长度最多60个字符'
                    }
                }
            },
        }
    }).on('success.form.bv', function (e) {
        // Prevent form submission
        e.preventDefault();
        if(!empty(aGET.copy)){
            app.copy();
        }else {
            app.update();
        }
    });
}